package com.ytjj.qmyx.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.kuaidi100.sdk.api.AutoNum;
import com.kuaidi100.sdk.response.AutoNumResp;
import com.kuaidi100.sdk.utils.PropertiesReader;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.admin.config.SupplyApiConfig;
import com.ytjj.qmyx.admin.dao.ExpressDao;
import com.ytjj.qmyx.admin.model.request.ExpressOrderRequest;
import com.ytjj.qmyx.admin.service.ExpressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 * @version 1.0
 *
 */
@Service
@Slf4j
public class ExpressServiceImpl implements ExpressService {

    @Resource
    private ExpressDao expressDao;
    @Resource
    private SupplyApiConfig supplyApiConfig;

    /**
     * 快递公司编码
     */
    private static Map<String, String> expressCompanyMap = null;

    @Override
    public CommonResult getExpressOrdersList(ExpressOrderRequest request) {
        request.setShopId(request.getShopId());
        String json = JSON.toJSONString(request);
        String resultJson = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/express/getExpressOrdersList", json, supplyApiConfig.getHeadsMap());
        if (StringUtils.isEmpty(resultJson)) {
            return null;
        }
        CommonResult result = JSONObject.parseObject(resultJson, CommonResult.class);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateExpressInfo(Integer expressId) {
        Map<String, String> map = new HashMap<>();
        map.put("expressId", String.valueOf(expressId));
        HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/express/updateExpressInfo", map, supplyApiConfig.getHeadsMap());
    }


    @Override
    public Map<String, String> getExpressCompanyNoList() {
        if (expressCompanyMap == null) {
            expressCompanyMap = Maps.newHashMap();
            List<Map> expressDicList = expressDao.getExpressDicList();
            if (expressDicList != null) {
                expressDicList.forEach(item -> {
                    expressCompanyMap.put(item.get("express_key").toString(), item.get("express_value").toString());
                });
            }
        }
        return expressCompanyMap;
    }

    public static String dateToStr(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = format.format(date);
        return str;
    }

    @Override
    public  List<Map<String, String>> queryExpressCompany(String num) {
        List<Map<String, String>> list = new ArrayList<>();
        AutoNum autoNum = new AutoNum();
        String key = PropertiesReader.get("key");
        List<AutoNumResp> comByNumList = autoNum.getComByNumList(key, num);
        if (!CollectionUtils.isEmpty(comByNumList)) {
            for (AutoNumResp autoNumResp : comByNumList) {
                Map<String, String> returnMap = new HashMap<>();
                returnMap.put("expressKey",autoNumResp.getName());
                returnMap.put("expressValue",autoNumResp.getComCode());
                list.add(returnMap);
            }
        }
        return list;
    }

}
